/**
 * 购物车
 */
function refreshData() {
	location.reload();
}

function changeCartGoodsCnt(goodsId, flag) {
	if(flag == "+") {
		var cnt = $("#goodsCnt_"+goodsId).val();
		if(ajaxChangGoodsCntInCart(goodsId,flag)) {
			++cnt;
			$("#goodsCnt_"+goodsId).val(cnt);
		}
	}
	if(flag == "-") {
		var cnt = $("#goodsCnt_"+goodsId).val();
		--cnt;
		if(cnt >= 1) {
			if(ajaxChangGoodsCntInCart(goodsId,flag)) {
				$("#goodsCnt_"+goodsId).val(cnt);
			}
		}else {
			deleteGoodsInCart(goodsId);
		}
	}
}

function ajaxChangGoodsCntInCart(goodsId,flag) {
	var options={path:'/',expires:1};
	var successFlag = false;
	$.ajax({
		url: 'ajaxChangGoodsCntInCart.action',
		data:{
			goodsId:goodsId,
			flag:flag
		},
		type:'post',
		async: false,
		dataType: 'text',
		success: function(msg) {
			if(msg == "failed") {
				alert("操作失败！");
			}else if(msg == "NotExist"){
				alert("对不起，该条商品记录不在购物车中！");
			}else {
				$.cookie("iFlowers_Shopping_Cart",msg,options);
				refreshData();
				successFlag = true;
			}
		}
	});
	return successFlag;
}

function updateTotalPrice(res) {
	var rs = res.result;
	var data = res.data;
	var totalPrice = 0;
	for(var i=0;i<data.length;i++) {
		totalPrice += data[i].goodsPrice * data[i].goodsCnt;
	}
	var totalPriceStr = '总计：￥'+totalPrice;
	$('#totalPrice').html(totalPriceStr);
}

function deleteGoodsInCart(goodsId) {
	if(confirm("确定要删除该条商品记录吗？")) {
		if(ajaxDeleteGoodsInCart(goodsId)) {
			alert("成功删除该条商品记录！");
			refreshData();
		}else {
			alert("删除失败！");
		}	
	}
}

function ajaxDeleteGoodsInCart(goodsId) {
	var options={path:'/',expires:1};
	var successFlag = false;
	$.ajax({
		url:'ajaxDeleteGoodsInCart.action',
		data:{
			goodsId:goodsId
		},
		type:'post',
		async:false,
		dataType:'text',
		success: function(msg) {
			if(msg == "failed") {
				alert("操作失败！");
			}else if(msg == "NotExist"){
				alert("对不起，购物车中不存在该条商品记录！");
			}else {
				$.cookie("iFlowers_Shopping_Cart",msg,options);
				successFlag = true;
			}
		}
	});
	return successFlag;
}




